home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bchelp10.zip / TI802.ASC < prev    next >
Text File  |  1991-09-11  |  2KB  |  67 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  C++                                    NUMBER  :  802
  9.   VERSION  :  ANY
  10.        OS  :  PC DOS
  11.      DATE  :  September 11, 1991                       PAGE  :  1/1
  12.  
  13.     TITLE  :  char ** Vs. char[][]
  14.  
  15.  
  16.  
  17.  
  18.   One of the pitfalls of the C language is based on the assumption
  19.   that char **apple, char *orange[], and char cherry[][] are
  20.   equivalent declarations. In practice, the types are virtually
  21.   identical, but some subtle differences can lead to unintended
  22.   results.
  23.  
  24.   'apple' is a pointer to a pointer, 'orange' is an array of
  25.   pointers, and 'cherry' is simply a two dimensional array. If
  26.   'apple' is declared as above, then expressions like apple[1], and
  27.   *apple[3] are valid because the compiler knows what the latter
  28.   expressions mean because it knows what type 'apple' really is.
  29.  
  30.   But, suppose the compiler had been told the incorrect type?  What
  31.   happens then?  Well, this can only happen if the variable is
  32.   defined in one module, say as 'char apple[][]' and declared
  33.   external in another, as 'extern char **apple'. In the second
  34.   module, when we use expressions for apple, the compiler will
  35.   interpret them as if apple were a char ** type, since that is how
  36.   it's declared (the compiler knows nothing of the definition in
  37.   module one). What this means for the implementation is this:
  38.   Let's compare char **apple with char cherry[][] 'apple' is the
  39.   label for a pointer, so to get at the pointer, the compiler
  40.   dereferences the label to get the first pointer.  Two more
  41.   dereferences will be required to get at the data now.  But
  42.   'cherry' is the label of an array, not a pointer.  So to access
  43.   an array member, the compiler needs to calculate the offset into
  44.   the array and use that as an offset of the label 'cherry'. There
  45.   is considerable difference between looking at the data offset
  46.   from a certain value, and dereferencing a sequence of pointers.
  47.   These differences will cause the data to appear corrupted, when
  48.   in reality, the data is fine and it is the measuring tool that is
  49.   incorrect.  The moral of this is, of course, to remember the
  50.   distinction between a char ** type and a char [][] type and not
  51.   to mix declarations of such types.
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.